package com.ms.education.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ms.education.course.entity.ClassUser;
import com.ms.education.course.entity.Classroom;
import com.ms.education.course.mapper.ClassUserMapper;
import com.ms.education.course.service.ClassUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ms
 * @since 2023-02-24
 */
@Service
public class ClassUserServiceImp extends ServiceImpl<ClassUserMapper, ClassUser> implements ClassUserService {

    @Override
    public List<Integer> getClassUserIds(Integer classId) {
        QueryWrapper<ClassUser> wrapper = new QueryWrapper<ClassUser>().eq("class_id", classId).select("user_id");
        return list(wrapper).stream().map(ClassUser::getUserId).collect(Collectors.toList());
    }

    @Override
    public void deleteClassUser(List<ClassUser> classUsers) {
        UpdateWrapper<ClassUser> wrapper = new UpdateWrapper<>();
        classUsers.forEach(cu -> {
            wrapper.or().eq("class_id", cu.getClassId()).eq("user_id", cu.getUserId());
        });
        remove(wrapper);
    }

    @Override
    public boolean isUserInClass(Integer classId, Integer userId) {
        QueryWrapper<ClassUser> wrapper = new QueryWrapper<ClassUser>().eq("class_id", classId).eq("user_id", userId);
        return getOne(wrapper) != null;
    }

    @Override
    public List<Classroom> getUserClass(Integer userId) {
        return baseMapper.getUserClass(userId);
    }
}
